WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
profiling_unit.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21#include <iostream>
22#include <iomanip>
23#include <chrono>
24#include <cmath>
25
26namespace wolf
27{
31{
32 protected:
33 bool running_;
35 unsigned int n_callbacks_;
37 std::chrono::microseconds acc_duration_us_;
38 std::chrono::microseconds max_duration_us_;
40 std::chrono::time_point<std::chrono::high_resolution_clock> first_clock_;
41 std::chrono::time_point<std::chrono::high_resolution_clock> start_clock_;
42
43 public:
48 ProfilingUnit(double desired_period_sec = -1);
49
52 void startProfiling();
53
56 void stopProfiling();
57
63 void printProfiling(const std::string& _tabs, std::ostream& stream = std::cout) const;
64
70 std::string printDuration(double _duration_microsec, int precision = 2) const;
71
74 void setDesiredPeriod(double _desired_period_sec);
75
78 bool isRunning() const;
79
82 unsigned int getNCallbacks() const;
83
86 double getDesiredPeriod() const;
87
90 double getAveragePeriod() const;
91
94 double getAccDuration() const;
95
98 double getAvgDuration() const;
99
102 double getMaxDuration() const;
103
106 double getVarDuration() const;
107
110 double getStDevDuration() const;
111};
112
113inline bool ProfilingUnit::isRunning() const
114{
115 return running_;
116}
117
118inline unsigned int ProfilingUnit::getNCallbacks() const
119{
120 return n_callbacks_;
121}
122
124{
125 return 1e-6 * desired_period_us_;
126}
127
128inline void ProfilingUnit::setDesiredPeriod(double _desired_period_sec)
129{
130 desired_period_us_ = 1e6 * _desired_period_sec;
131}
132
134{
135 return 1e-6 * average_period_us_;
136}
137
138inline double ProfilingUnit::getAccDuration() const
139{
140 return 1e-6 * acc_duration_us_.count();
141}
142
143inline double ProfilingUnit::getAvgDuration() const
144{
145 return (n_callbacks_ > 0 ? 1e-6 * acc_duration_us_.count() / n_callbacks_ : 0.0);
146}
147
148inline double ProfilingUnit::getMaxDuration() const
149{
150 return 1e-6 * max_duration_us_.count();
151}
152
153inline double ProfilingUnit::getVarDuration() const
154{
155 return 1e-12 * var_duration_us_;
156}
157
159{
160 return 1e-6 * sqrt(var_duration_us_);
161}
162
163} // namespace wolf
Profiling of a periodic task: number of calls, duration (accumulated, average, max,...
Definition: profiling_unit.h:31
std::chrono::time_point< std::chrono::high_resolution_clock > start_clock_
Time point of the start clock.
Definition: profiling_unit.h:41
double desired_period_us_
Desired period in microseconds (< 0 for not specified)
Definition: profiling_unit.h:34
double getAvgDuration() const
Returns the average duration in seconds.
Definition: profiling_unit.h:143
double getMaxDuration() const
Returns the maximum duration in seconds.
Definition: profiling_unit.h:148
double getStDevDuration() const
Returns the standard deviation of the duration in seconds.
Definition: profiling_unit.h:158
unsigned int getNCallbacks() const
Returns the number of profiled calls.
Definition: profiling_unit.h:118
unsigned int n_callbacks_
Number of callbacks.
Definition: profiling_unit.h:35
double getVarDuration() const
Returns the variance of the duration in seconds squared.
Definition: profiling_unit.h:153
void setDesiredPeriod(double _desired_period_sec)
Sets the desired period in seconds.
Definition: profiling_unit.h:128
double getAccDuration() const
Returns the accumulated duration in seconds.
Definition: profiling_unit.h:138
std::chrono::microseconds max_duration_us_
Maximum duration in microseconds.
Definition: profiling_unit.h:38
ProfilingUnit(double desired_period_sec=-1)
Constructor.
Definition: profiling_unit.cpp:24
double var_duration_us_
Variance of the duration in microseconds squared.
Definition: profiling_unit.h:39
void startProfiling()
Starts the profiling of a call.
Definition: profiling_unit.cpp:35
void printProfiling(const std::string &_tabs, std::ostream &stream=std::cout) const
Prints the profiling results.
Definition: profiling_unit.cpp:107
std::chrono::time_point< std::chrono::high_resolution_clock > first_clock_
Time point of the first clock.
Definition: profiling_unit.h:40
double getAveragePeriod() const
Returns the average period in seconds.
Definition: profiling_unit.h:133
std::chrono::microseconds acc_duration_us_
Accumulated duration in microseconds.
Definition: profiling_unit.h:37
std::string printDuration(double _duration_microsec, int precision=2) const
Returns a string of a duration (in us) in the most appropriate units with the specified precision.
Definition: profiling_unit.cpp:80
void stopProfiling()
Stops the profiling of a call.
Definition: profiling_unit.cpp:44
double getDesiredPeriod() const
Returns the desired period in seconds.
Definition: profiling_unit.h:123
bool running_
True if profiling is running.
Definition: profiling_unit.h:33
bool isRunning() const
Returns whether the profiling of a call is running.
Definition: profiling_unit.h:113
double average_period_us_
Average period in microseconds.
Definition: profiling_unit.h:36
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:617
Definition: capture_base.h:23